<template>
  <div class="container">
    <el-form :model="formParams" ref="form" :rules="rules" label-position="top" size="normal">
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="上级菜单">
            <el-cascader :options="menuType" v-model="formParams.parent_ids" clearable :props="{ checkStrictly: true }"
              class="wid">
            </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="0">
          <el-form-item label="可见性">
            <el-radio-group v-model="formParams.is_show">
              <el-radio-button value="1" label="1">显示</el-radio-button>
              <el-radio-button value="0" label="0">隐藏</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="0">
          <el-form-item label="类型">
            <el-radio-group v-model="formParams.type">
              <el-radio-button value="1" label="1">菜单</el-radio-button>
              <el-radio-button value="2" label="2">按钮</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8" :offset="0">
          <el-form-item label="名称" prop="name">
            <el-input v-model="formParams.name" placeholder="必填"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item label="路由">
            <el-input v-model="formParams.path"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="formParams.sort" placeholder="必填"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <label>接口权限</label>
        <el-button type="primary" size="medium" class="ml10" @click="power">添加</el-button>
        <el-select v-model="permissions" value-key="permission" clearable multiple class="wid">
          <el-option v-for="item in permissionType" :key="item.value" :label="item.label+'_'+item.value"
            :value="item.value">
            {{item.label}}_{{item.value}}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input v-model="formParams.remarks" type="textarea" :rows="4"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="center">
          <el-button type="danger" size="medium" @click="cancel">取消</el-button>
          <el-button type="primary" size="medium" @click="onEdit" v-if="this.formParams.id" :loading="loading">更新
          </el-button>
          <el-button type="primary" size="medium" @click="onAdd" v-else :loading="loading">新增</el-button>
        </div>
      </el-form-item>
    </el-form>
    <!-- 接口权限 -->
    <el-dialog title="修改权限接口" :visible.sync="powerShow" width="70%" @close="powerShow = false" append-to-body
      :destroy-on-close="true">
      <permission :permissionType="permissionType" @powerName="powerName"></permission>
    </el-dialog>
  </div>
</template>
<script>
import Permission from './permission.vue'
export default {
  name: 'MenuEdit',
  components: {
    Permission

  },
  props: {
    formParams: {
      type: Object,
      default: () => {
        return {
          is_show: '0',
          type: '1'
        }
      }
    },
    menuType: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      loading: false,
      permissionType: [],
      powerShow: false,
      permissions: [],
      rules: {
        sort: [
          { required: true, message: '序号必填', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '菜单名称必填', trigger: 'blur' }
        ],
      }
    }
  },
  created () {

  },
  mounted () {
    this.getPermission()
  },
  methods: {
    cancel () {
      this.$emit('cancel');
    },
    // 获取接口权限
    async power () {
      this.powerShow = true
    },
    // 权限回显
    async getPermission () {
      // 获取菜单的权限字段
      const res = await this.$http('sysPermission', {
        permission: this.formParams.permission
      })
      res.forEach(item => {
        item.value = item.id
        item.label = item.name
      })
      this.permissionType = res
      // 解构回显权限字段
      let res1 = []
      if (this.formParams.permission) {
        let str = this.formParams.permission
        res1 = str.split(',')
      }
      this.permissions = res1
    },
    // 权限回传解析
    powerName (arr) {
      this.powerShow = false
      this.formParams.permission = this.$util.arrToStr(arr, 'id')
      let res1 = []
      if (this.formParams.permission) {
        let str = this.formParams.permission
        res1 = str.split(',')
      }
      this.permissions = res1
    },
    // 新增 parent_id ，修改 parentId
    async onAdd () {
      this.loading = true
      if (!this.check()) {
        this.loading = false
        return false
      }
      let pid = this.formParams.parent_ids[this.formParams.parent_ids.length - 1]
      if (pid == this.formParams.id) {
        this.$tips.error('不可以选择自己为父节点')
        return
      }
      this.$confirm('是否确认新增', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async (action) => {
        let power = this.$util.arrToStr(this.permissions)
        const res = await this.$http('sysMenuUp', {
          id: this.formParams.id,
          isShow: this.formParams.is_show,
          is_show: this.formParams.is_show,
          name: this.formParams.name,
          parent_id: pid,
          path: this.formParams.path,
          permission: power,
          remarks: this.formParams.remarks,
          sort: this.formParams.sort,
          type: this.formParams.type,
        })
        this.loading = false
        this.$tips.success(res.errmsg)
        this.$emit('init');
        this.$emit('cancel');
      }).catch(() => {
        this.loading = false
      });
    },
    async onEdit () {
      this.loading = true
      if (!this.check()) {
        this.loading = false
        return false
      }
      let pid = this.formParams.parent_ids[this.formParams.parent_ids.length - 1]
      let removePid = {}
      // 传递parent_id 父菜单会更改 且 分配角色菜单消失
      if (pid != this.formParams.pid) {
        removePid.parent_id = pid
      }
      if (pid == this.formParams.id) {
        this.$tips.error('不可以选择自己为父节点')
        return
      }
      this.$confirm('是否确认更改', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async (action) => {
        let pids = '0,' + this.$util.arrToStr(this.formParams.parent_ids) + ','
        let power = this.$util.arrToStr(this.permissions)
        const res = await this.$http('sysMenuUp', {
          ...removePid,
          parentId: pid,
          id: this.formParams.id,
          isShow: this.formParams.is_show,
          is_show: this.formParams.is_show,
          name: this.formParams.name,
          parent_ids: pids,
          path: this.formParams.path,
          permission: power,
          remarks: this.formParams.remarks,
          sort: this.formParams.sort,
          type: this.formParams.type
        })
        this.loading = false
        this.$tips.success(res.errmsg)
        this.$emit('cancel');
        this.$emit('init');
      }).catch(() => {
        this.loading = false
      });
    },
    check () {
      if (!this.formParams.sort) {
        this.$tips.error('序号必填')
        return false
      }
      const regex = /^\+?[1-9]\d*$/
      if (!regex.test(this.formParams.sort)) {
        this.$tips.error('序号应为正整数')
        return false
      }
      if (!this.formParams.name) {
        this.$tips.error('菜单名称必填')
        return false
      }
      return true
    }
  },
  computed: {

  },
  watch: {

  }
}
</script>
<style lang='scss' scoped>
</style>
